package com.jrx.anytxn.reporting.job.lsPuHui.config;

import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.job.lsPuHui.step.BreakContractListener;
import com.jrx.anytxn.reporting.job.lsPuHui.step.BreakContractProcessor;
import com.jrx.anytxn.reporting.job.lsPuHui.step.BreakContractReader;
import com.jrx.anytxn.reporting.job.lsPuHui.step.BreakContractWriter;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成 个人违约信息文件(全量)
 * 逾期天数大于等于90天的数据(没有也送空文件)，渠道号_CopyBreakContractInfo.dat
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateBreakContractConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private BreakContractListener breakContractListener;
    @Autowired
    private BreakContractProcessor breakContractProcessor;
    @Autowired
    private BreakContractWriter breakContractWriter;


    @Bean
    public Job breakContractFileJob() {
        return jobBuilderFactory.get("breakContractFileJob")
                .incrementer(new RunIdIncrementer()).listener(breakContractListener)
                .start(breakContractStep())
                .build();
    }

    @Bean
    public Step breakContractStep() {
        return stepBuilderFactory.get("breakContractStep")
                .<AmLoanAccountInfo, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(breakContractReader())
                .processor(breakContractProcessor)
                .writer(breakContractWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<AmLoanAccountInfo> breakContractReader(){
        return new BreakContractReader(sqlSessionFactory,anyTxnBatchProperties.getFileConfig().getPageSize());
    }

}
